<template>
	<view>
		<u-form label-width="150rpx" style="padding: 20rpx 20rpx 20rpx 50rpx;">
			<u-form-item label="帐号" ><u-input v-model="phone" placeholder="请输入帐号"/></u-form-item>
			<u-form-item label="密码" ><u-input v-model="passWord" placeholder="请输入密码"/></u-form-item>
		</u-form>
		<view style="color:#ff0000; text-align: right; padding: 10rpx 50rpx 20rpx 10rpx;" @click="lossPassWord">忘记密码？</view>
		<u-row gutter="16" style="padding: 10rpx 10rpx 60rpx 10rpx;" >
			<u-col span="2">
				<span>&nbsp;</span>
			</u-col>
			<u-col span="3">
				<u-button type="info" size="medium" @click="canel">返回</u-button>
			</u-col>
			<u-col span="2">
				<span>&nbsp;</span>
			</u-col>
			<u-col span="3">
				<u-button type="primary" size="medium" @click="login">登录</u-button>
			</u-col>
			<u-col span="2">
				<span>&nbsp;</span>
			</u-col>
		</u-row>
		<view style="text-align: center; padding: 100rpx 10rpx 50rpx 10rpx;">
			<span  @click="lossPassWord">还没有账户，</span>
			<span style="color:#ff0000; text-align: right; padding: 10rpx 50rpx 20rpx 10rpx;" @click="toRegister">立即注册</span>
		</view>
		
		<view>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phone : "",
				passWord : "",
			}
		},
		methods:{
			// 同步数据
			login(){
				if(this.phone === "" || this.phone === null || this.phone === undefined){
					this.$refs.uToast.show({
						title: '请输入帐号',
						type: 'error'
					});
					return;
				}
				if(this.passWord === "" || this.passWord === null || this.passWord === undefined){
					this.$refs.uToast.show({
						title: '请输入密码',
						type: 'error'
					});
					return;
				}
				this.$u.post('/ledger/member/login', {
								phone: this.phone,
								passWord: this.passWord
							}).then(data => {
								if (data && data.code === 0) {
									this.$refs.uToast.show({title: "登录成功,即将跳转到我的主页", type: 'success',});
									this.$u.vuex('myConfig.userId', data.userInfo.userId);
									this.$u.vuex('myConfig.userName', data.userInfo.userName);
									this.$u.vuex('myConfig.img', data.userInfo.img);
									this.$u.vuex('myConfig.phone', data.userInfo.phone);
									
									// this.$u.vuex('myConfig.passWord', data.userInfo.passWord);
									this.sync(data.userInfo.userId);
									setTimeout(this.canel, 1000);
								} else {
									this.$refs.uToast.show({title: data.msg, type: 'error',});
								}
							}).catch((e) => {
								this.$refs.uToast.show({title: "手机好累哦^_^", type: 'error',});
							});

			},
			// 返回我的页面
			canel(){
				this.$u.route({
					url : '/pages/my/index',
					type: "tab"
				})
			},
			// 忘记密码
			lossPassWord(){
				this.$refs.uToast.show({
					title: '请联系管理员，手机&微信13931854366',
					type: 'success'
				})
			},
			// 跳转到注册页面
			toRegister(){
				this.$u.route({
					url : '/pages/my/register'
				})
			},
			// 同步数据
			sync(userId){
				if(userId === null 
					|| userId === undefined
					|| userId === ""){
						return;
					}
				this.$u.post('/ledger/record/sync', {
							userId: userId,
							log: JSON.stringify(this.recordList)
						}).then(data => {
							if (data && data.code === 0) {
								this.$u.vuex('recordList', data.recordList);
							} 
						});
			
			},
		}
	}
</script>